
import java.awt.Color;
import java.util.Arrays;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sewardn
 */
public class Test {



    public void forward(Turtle t,String k)
    {
        t.forward(10);
    }
    public void left(Turtle t,String k)
    {
        t.left(30);
    }
    public void right(Turtle t,String k)
    {
        t.left(-30);
    }
    public void purple(Turtle t,String k)
    {
        t.outlineColor("purple");
    }
    public void undo(Turtle t,String k)
    {
        t.undo(10);
    }
    public void redo(Turtle t,String k)
    {
        t.redo(10);
    }
    public void zoom(Turtle t,String k)
    {
        t.zoomFit();
    }
    public static void faster(Turtle t,String k)
    {
        if (t.getSpeed()>10)t.speed(t.getSpeed()-10);
        else t.speed(0);
    }
    public void slower(Turtle t,String k)
    {
        t.speed(t.getSpeed()+10);
    }
    public void stamp(Turtle t)
    {
        System.out.println("stamp");
        t.stamp();
        t.shape("arrow");
    }
    public void dot(Turtle t)
    {
        System.out.println("dot");
        t.dot();
    }
    public void update(Turtle t)
    {
        t.update();
    }
    public void rcolor(Turtle t)
    {
        int r=(int)(Math.random()*255);
        int g=(int)(Math.random()*255);
        int b=(int)(Math.random()*255);
        Turtle.bgcolor(new Color(r,g,b));
    }
    public void clear(Turtle t)
    {
        try
        {
            t.clear();
        }
        catch (Exception e) {e.printStackTrace();}
    }

    public void release(Turtle t, String key) throws Exception
    {
        while(Turtle.isKeyDown(key))
        {
            Thread.sleep(100);//Wait for the key to be released
        }
        t.forward(100);//do something
        t.exit();
    }

    private static void hilbert(int n,Turtle t)
    {
        if (n == 0) return;
        t.penColor("orange");
        //t.down();
        t.left(90);
        treblih(n-1,t);
        t.forward(10);
        t.left(-90);
        hilbert(n-1,t);
        t.forward(10);
        hilbert(n-1,t);
        t.left(-90);
        t.forward(10);
        treblih(n-1,t);
        t.left(90);
    }

    // evruc trebliH
    public static void treblih(int n,Turtle t) {
        if (n == 0) return;
        t.penColor("blue");
        //t.up();
        t.left(-90);
        hilbert(n-1,t);
        t.forward(10);
        t.left(90);
        treblih(n-1,t);
        t.forward(10);
        treblih(n-1,t);
        t.left(90);
        t.forward(10);
        hilbert(n-1,t);
        t.left(-90);
    }

    public static void main(String[] a)
    {
        Turtle t=new Turtle(-200,-200);
        t.bgpic("escher.gif");
        //t.shape("turtle.png");
        //Turtle t2=new Turtle(200,200);
        //t2.left(180);
        //t2.forward(100);
        //t2.onKey("forward","l");
        t.width(3);
        t.onKey("stamp", "s");
        t.onKey("dot", "d");
        t.onKey("update", "q", false, true);
        t.onKey("rcolor", "w", false, true);
        t.onKey("faster", "numpad +", false, true);
        t.onKey("slower", "numpad -", false, true);
        t.onKey("forward", "up", false, true);
        t.onKey("left", "left");
        t.onKey("right", "right");
        t.onKey("purple", "p");
        t.onKey("zoom", "z", false, true);
        t.onKey("undo", "u", false, true);
        t.onKey("redo", "r", false, true);
        t.onKey("clear", "c");
        t.onKey("release", "m");
        hilbert(5,t);
        t.exit();
        System.out.println("asdf");
    }
}
